package com.haoting.blogcms.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haoting.blogcms.entity.Banner;
import com.haoting.blogcms.service.BannerService;
import com.haoting.commonutils.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tingting
 * @since 2021-01-04
 */
@RestController
@RequestMapping("/blogcms/banneradmin")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("pageBanner/{page}/{limit}")
    public CommonResult pageBanner(@PathVariable long page,@PathVariable long limit){
        Page<Banner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        return CommonResult.ok().data("itemls",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    //2 添加banner
    @PostMapping("addBanner")
    public CommonResult addBanner(@RequestBody Banner crmBanner) {
        bannerService.save(crmBanner);
        return CommonResult.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public CommonResult get(@PathVariable String id) {
        Banner banner = bannerService.getById(id);
        return CommonResult.ok().data("item", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public CommonResult updateById(@RequestBody Banner banner) {
        bannerService.updateById(banner);
        return CommonResult.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public CommonResult remove(@PathVariable String id) {
        bannerService.removeById(id);
        return CommonResult.ok();
    }
}

